#include <QModelIndex>
#include <QApplication>

#include "delegate.h"
#include "Task.h"

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                  const QModelIndex &index ) const {

    if (index.column() != PROGRESS) {
        QItemDelegate::paint(painter, option, index);
        return;
    }

    int progress = index.model()->data(index, Qt::DisplayRole).toInt();
    QStyleOptionProgressBar progress_bar = createProgressBar(progress, option);
    QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progress_bar, painter);
}


QStyleOptionProgressBar Delegate::createProgressBar(int progress, const QStyleOptionViewItem &option) const {
    QStyleOptionProgressBar progress_bar;
    progress_bar.state = QStyle::State_Enabled;
    progress_bar.direction = QApplication::layoutDirection();
    progress_bar.rect = option.rect;
    progress_bar.fontMetrics = QApplication::fontMetrics();
    progress_bar.minimum = 0;
    progress_bar.maximum = 100;
    progress_bar.textAlignment = Qt::AlignCenter;
    progress_bar.textVisible = true;
    progress_bar.progress = progress < 0 ? 0 : progress;
    progress_bar.text = QString().sprintf("%d%%", progress_bar.progress);
    return progress_bar;
}


